package week05;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class CampAutoConfigurationTestWithContextRunner {
    private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
            .withConfiguration(AutoConfigurations.of(CampAutoConfiguration.class));

    @Test
    public void testStarter() {
        contextRunner.withPropertyValues("camp.started=true", "camp.date=2021-07-23", "camp.week=5").run(context -> {
            CampService service = context.getBean("camp", CampService.class);
            assertEquals(service, context.getBean("camp"));
            assertEquals(service.getWeek(), 5);
            assertEquals(service.getDate(), "2021-07-23");
        });
        contextRunner.withPropertyValues("camp.started=false", "camp.date=2021-07-23", "camp.week=5").run(context -> {
            assertThrows(NoSuchBeanDefinitionException.class, () -> context.getBean("camp", CampService.class));
        });
    }
}
